package com.apache.website.service.impl;

import java.util.List;

import com.apache.api.vo.ParamsVo;
import com.apache.cache.util.Validator;
import com.apache.database.db.IDao;
import com.apache.database.model.MethodParam;
import com.apache.database.model.Page;
import com.apache.exception.BusinessException;
import com.apache.website.entity.Channel;
import com.apache.website.manager.ChannelManager;

public class ChannelManagerImpl implements ChannelManager {

	private IDao channelDao;
	private final String entityName = "com.apache.website.entity.Channel";

	public void setChannelDao(IDao channelDao) {
		this.channelDao = channelDao;
	}

	public Page getPageInfo(ParamsVo<Channel> vo) {
		String orderBy = vo.getKey();
		if (Validator.isNull(orderBy))
			orderBy = "CREATE_TIME";
		MethodParam param = setMethodParams(vo, 2);
		int pageSize = Integer.valueOf(Validator.getDefaultStr(String.valueOf(vo.getParams("pageSize")), "10"));
		int pageIndex = Integer.valueOf(Validator.getDefaultStr(String.valueOf(vo.getParams("pageIndex")), "1"));
		param.setPageIndex(pageIndex);
		param.setPageSize(pageSize);
		//	param.setOrderby(orderBy + " desc");
		Page page = channelDao.pageSelect(param);
		return page;
	}

	public String saveInfo(ParamsVo<Channel> vo) throws BusinessException {
		Channel org = vo.getObj();
		String infoId = Validator.generate();
		//定义对象缓存KEY,如果不需要缓存对象请不要对变量赋值，如果要缓存对象建议使用infoId
		String cacheKey = "";
		org.setChannelId(infoId);
		MethodParam param = new MethodParam("Channel", cacheKey, "", entityName);
		param.setVaule(org);
		if (channelDao.insert(param)) {
			return infoId;
		}
		return "";
	}

	public boolean editInfo(ParamsVo<Channel> vo) throws BusinessException {
		Channel org = vo.getObj();
		if (Validator.isNotNull(org.getChannelId())) {
			String cacheKey = "";
			MethodParam param = new MethodParam("Channel", cacheKey, "", entityName);
			param.setVaule(org);
			return channelDao.edit(param);
		}
		return false;
	}

	public boolean deleteInfo(ParamsVo<Channel> vo) throws BusinessException {
		String infoId = vo.getInfoId();
		if (Validator.isNull(infoId)) {
			return false;
		}
		String mark = Validator.getDefaultStr(String.valueOf(vo.getParams("isDelete")), "true");
		MethodParam param = new MethodParam("ById", "", "", entityName);
		param.setParams("channelId", infoId);
		if ("true".equals(mark))
			param.setDelete(true);
		boolean flag = channelDao.delete(param);
		return flag;
	}

	public Object execute(ParamsVo<Channel> vo) {
		String key = vo.getKey();
		if ("checkChannelCode".equals(key)) {
			Channel cl = vo.getObj();
			if (Validator.isEmpty(getChannelByCode(cl.getCodeName()))) {
				return true;
			}
			return false;
		} else if ("ByCodeName".equals(key)) {
			return getChannelByCode(String.valueOf(vo.getParams("codeName")));
		}
		return null;
	}

	private Channel getChannelByCode(String codeName) {
		MethodParam param = new MethodParam("ByCodeName", "", "", entityName);
		param.setParams("codeName", codeName);
		return (Channel) channelDao.selectSingle(param);
	}

	public Object getInfoById(ParamsVo<Channel> vo) {
		String infoId = vo.getInfoId();
		String cacheKey = "";
		if (Validator.isNull(infoId)) {
			return null;
		}
		MethodParam param = new MethodParam("ById", cacheKey, "", entityName);
		param.setInfoId(infoId);
		return channelDao.selectById(param);
	}

	public List<Channel> getList(ParamsVo<Channel> vo) {
		String orderBy = vo.getKey();
		if (Validator.isNull(orderBy))
			orderBy = "CREATE_TIME";
		MethodParam param = setMethodParams(vo, 2);
		//param.setOrderby(orderBy + " desc");
		return channelDao.select(param);
	}

	public long countInfo(ParamsVo<Channel> vo) {
		MethodParam param = setMethodParams(vo, 1);
		return channelDao.count(param);
	}

	private MethodParam setMethodParams(ParamsVo<Channel> vo, int type) {
		String methodKey = Validator.getDefaultStr(vo.getMethodKey(), "ByProperty");
		MethodParam param = new MethodParam(methodKey, "", "", entityName);
		Channel org = vo.getObj();
		if (Validator.isEmpty(org))
			return param;
		if (Validator.isNotNull(org.getChannelType())) {
			param.setParams("channelType", org.getChannelType());
		}
		if (Validator.isNotNull(org.getParentId())) {
			param.setParams("parentId", org.getParentId());
		}
		if (Validator.isNotNull(org.getChannelName())) {
			param.setParams("channelName", "'%" + org.getChannelName() + "%'");
		}
		if (Validator.isNotNull(org.getDisplayName())) {
			param.setParams("displayName", "'%" + org.getDisplayName() + "%'");
		}
		if (Validator.isNotNull(org.getStatus())) {
			param.setParams("status", org.getStatus());
		}
		if (Validator.isNotNull(org.getIsNavigation())) {
			param.setParams("isNavigation", org.getIsNavigation());
		}
		if (Validator.isNotNull(org.getIsPushed())) {
			param.setParams("isPushed", org.getIsPushed());
		}
		if (Validator.isNotNull(org.getCodeName())) {
			param.setParams("codeName", org.getCodeName());
		}
		if (Validator.isNotNull(org.getCreateUser())) {
			param.setParams("createUser", org.getCreateUser());
		}
		if (Validator.isNotNull(org.getPublisher())) {
			param.setParams("publisher", org.getPublisher());
		}
		if (Validator.isNotNull(org.getPublisher())) {
			param.setParams("createTime", org.getCreateTime());
		}
		return param;
	}
}
